package com.zhiyou.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhiyou.model.BookType;
import com.zhiyou.service.BookTypeService;
import com.zhiyou.utils.JsonResult;

@RestController
@RequestMapping("BookType/")
public class BookTypeController {

	@Autowired
	BookTypeService bookTypeService;
	/**
	 * 添加图书类型
	 * @param bookType
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("add")
	public JsonResult add(BookType bookType,HttpServletRequest request,HttpServletResponse response){
		try {
			bookTypeService.add(bookType);
			return new JsonResult("插入类型成功");
		} catch (Exception e) {
			return new JsonResult(500,"插入失败");
		}
	}
	
	/**
	 * 更新图书类型
	 * @param bookType
	 * @param request
	 * @param response
	 * @return
	 */
	@PutMapping("update")
	public JsonResult update(BookType bookType,HttpServletRequest request,HttpServletResponse response){
		try {
			bookTypeService.update(bookType);;
			return new JsonResult("修改类型成功");
		} catch (Exception e) {
			return new JsonResult(500,"修改失败");
		}
	}
	
	/**
	 * 删除图书类型
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@DeleteMapping("delete/{id}")
	public JsonResult delete(@PathVariable("id")int id,HttpServletRequest request,HttpServletResponse response){
		try {
			bookTypeService.delete(id);
			return new JsonResult("删除类型成功");
		} catch (Exception e) {
			return new JsonResult(500,"删除失败");
		}
	}
	/**
	 * 批量删除图书类型
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("delete")
	public JsonResult deleteAll(int[] ids,HttpServletRequest request,HttpServletResponse response){
		try {
			bookTypeService.deleteAll(ids);;
			return new JsonResult("批量删除类型成功");
		} catch (Exception e) {
			return new JsonResult(500,"删除失败");
		}
	}
	/**
	 * 根据图书类型id查询图书类型信息
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("{id}")
	public JsonResult selectById(@PathVariable("id")int id,HttpServletRequest request,HttpServletResponse response){
		try {
			BookType selectById = bookTypeService.selectById(id);
			return new JsonResult(selectById,"查询类型详细信息成功");
		} catch (Exception e) {
			return new JsonResult(500,"查询失败");
		}
	}
	
	/**
	 * 查询全部图书类型信息
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("list")
	public JsonResult list(HttpServletRequest request,HttpServletResponse response){
		try {
			List<BookType> list = bookTypeService.selectAll();
			return new JsonResult(list,"查询类型成功");
		} catch (Exception e) {
			return new JsonResult(500,"查询失败");
		}
	}
}
